package com.example.juc;

import java.util.concurrent.atomic.AtomicMarkableReference;

class Book3 {
    private String title;
    private double price;

    public Book3(String title, double price) {
        this.title = title;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book3{" +
                "title='" + title + '\'' +
                ", price=" + price +
                '}';
    }
}

// ABA 问题
public class AtomicMarkableReferenceTest {
    public static void main(String[] args) {
        Book book = new Book("JAVA JUC", 68.8);
        Book book2 = new Book("JDBC", 18.8);
        AtomicMarkableReference<Book> ref = new AtomicMarkableReference<>(book, true);
        System.err.println("[x引用内容修改]" + ref.compareAndSet(book, book2, false, true));
        System.err.println("[x原子引用数据]" + ref.isMarked() + "、存储内容: " + ref.getReference());

        System.err.println("[√引用内容修改]" + ref.compareAndSet(book, book2, true, false));
        System.err.println("[√原子引用数据]" + ref.isMarked() + "、存储内容: " + ref.getReference());
    }
}
